package vn.demand.utils
{
	import com.adobe.utils.StringUtil;
	
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.formatters.DateFormatter;
	import mx.formatters.NumberFormatter;
	
	import vn.demand.dragon.CommonCommand;
	
	public class DpUtils
	{
		private static var df:DateFormatter = null; 
		
		public static function get dateFormatter() : DateFormatter {
			if (null == df) {
				df = new DateFormatter();
	        	df.formatString = 'DD-MM-YYYY';
			}
			return df;
		}
		
		public function DpUtils()
		{
		}
		
		public static function lfNumberValue(value:Object, currency:int=5):String {
			if (Number(value) == NaN || Number(value) == 0) {
				return "";
			}
			var nf:NumberFormatter = null;
			if (currency == 5) {
				nf = CommonCommand.vndNumberFormatter;
			} else {
				nf = CommonCommand.usdNumberFormatter;
			}
			return nf.format(value);
		}
		
		public static function exchangeCurrency(value:Number, exchangeRate:Number, currency:int):Number {
			if (currency == 5) {
				return value;
			}
			return value * exchangeRate;
		}
		
		public static function lfBooleanValue(item:Object, column:AdvancedDataGridColumn):String {
        	if (item.hasOwnProperty(column.dataField)) {
        		var bValue = item[column.dataField];
	        	return bValue == true || Number(bValue) > 0 ? "X" : "";
        	}
        	return "";
        }
		
		public static function isTrue(bValue:Object):Boolean {
			return bValue == true || Number(bValue) > 0 ;
		}
		
		public static function lfDateValue(item:Object, column:AdvancedDataGridColumn):String {
        	if (item.hasOwnProperty(column.dataField)) {
	            return dateFormatter.format(parseDate(item[column.dataField]));
        	}
        	return "";
        }
		
		public static function compare(d1:Date, d2:Date, allowNull:Boolean=true):Number {
			if (!allowNull && (d1 == null || d2 == null)) {
				throw new Error("Do not allow null in params");
			}
			var t1:Number = 0;
			var t2:Number = 0;
			if (d1 != null) {
				t1 = d1.getTime();
			}
			if (d2 != null) {
				t2 = d2.getTime();
			} else if (allowNull) {
				t2 = t1;
			}
			return t2 - t1;
		}
		 /**
                         * Formats a string to date using the mx.formats.DateFormmatter
formatString
                         */
                        public static function parseDate(str:String, format : String = "YYYY-MM-DD" ) : Date
                        {
                        	if (str == null || StringUtil.trim(str) == '') {
                        		return null;
                        	}
                                var dateObj : Date;
                                var first : int;
                                var last : int;

                                //Create the date
                                dateObj = new Date();

                                //Get the year
                                //If the number of pattern letters is two, the year is truncated to two digits;
                                //otherwise, it appears as four digits.
                                first = format.indexOf( "Y" );
                                last = format.lastIndexOf( "Y");

                                if ( first == -1 )
                                {
                                        throw new Error("Parse error. Year is requiered");
                                }
                                else if ( last - first == 1 )
                                {
                                        dateObj.fullYear = 2000 +  Number(str.substr( first, (last -
first) + 1 ));
                                }
                                else
                                {
                                        dateObj.fullYear = Number(str.substr( first, (last - first) +
1 ));
                                }

                                //Get the month (from 0 to 12)
                                first = format.indexOf( "M" );
                                last = format.lastIndexOf( "M" );
                                trace("Month - first: " + first + " last : " + last);
                                if ( first == -1 )
                                {
                                        throw new Error("Parse error. Month is requiered");
                                }
                                else if ( last - first != 1)
                                {
                                        throw new Error("Only two letter month pattern is supported.");
                                }
                                //Month in date object is between 0 - 11
                                dateObj.month = Number( str.substr( first, 2 ) ) - 1;

                                //Get the day
                                first = format.indexOf( "D" );
                                last = format.lastIndexOf( "D" );
                                //Only one letter in the pattern
                                if ( first == -1 )
                                {
                                        throw new Error("Parse error. Day is requiered");
                                }
                                else if ( last == first )
                                {
                                        dateObj.date = Number( str.substr( first, 1 ) );
                                }
                                else if ( last - first == 1)
                                {
                                        dateObj.date = Number( str.substr( first, 2 ) );
                                }
                                else
                                {
                                        throw new Error("Parse error.");
                                }

                                //Set the hour, minute, second, ms
                                //Hour 1 -24
                                first = format.indexOf( "H" );
                                last = format.lastIndexOf( "H" );
                                if ( first != -1 )
                                {
                                        if ( last == first )
                                        {
                                                throw new Error("Parse Error. Wrong hour format");
                                        }
                                        else
                                        {
                                                //Date object hours is between 0 and 23
                                                dateObj.setHours(  Number( str.substr( first, 2 ) ) -1 );
                                        }
                                }
                                //Hour 0 - 23
                                first = format.indexOf( "J" );
                                last = format.lastIndexOf( "J" );
                                if ( first != -1 )
                                {
                                        if ( last == first )
                                        {
                                                throw new Error("Parse Error. Wrong hour format");
                                        }
                                        else
                                        {
                                                dateObj.setHours(  Number( str.substr( first, 2 ) ) );
                                        }
                                }
                                //Hour in AM PM
                                first = format.indexOf( "A" );
                                last = format.lastIndexOf( "A" );
                                if ( first != -1 )
                                {
                                        //If AM/PM is set search for the current hour
                                        //JJ or KK
                                       //TODO
                                }

                                //Minutes
                                first = format.indexOf( "N" );
                                last = format.lastIndexOf( "N" );
                                if ( first != -1 )
                                {
                                        if ( last == first )
                                        {
                                                dateObj.setMinutes( Number( str.substr( first, 1 ) ) );
                                        }
                                        else
                                        {
                                                dateObj.setMinutes( Number( str.substr( first, 2 ) ) );
                                        }
                                }

                                //Seconds
                                first = format.indexOf( "S" );
                                last = format.lastIndexOf( "S" );
                                if ( first != -1 )
                                {
                                        if ( last == first )
                                        {
                                                dateObj.setMinutes( Number( str.substr( first, 1 ) ) );
                                        }
                                        else
                                        {
                                                dateObj.setMinutes( Number( str.substr( first, 2 ) ) );
                                        }
                                }

                                return dateObj;
                        } 

	}
}